var yfmbgl_app = function() {
	var path = Constants.amUrlPath;
	var ractive;
	var csz = "";
	var ywy = "";
	var jfye = "";
	function renderView() {
		// 积分比例100：1
		csz = manager.findParamFromUrl("csz");
		ywy = manager.findParamFromUrl("ywy");
		jfye = manager.findParamFromUrl("jfye");

		ractive = new Ractive({
			el : ".hp-container",
			template : "#main-template",
			onrender : function() {
			},
			oncomplete : function() {
				ractive.set("csz", csz);
				// 设置可兑换现金：
				// 设置默认可以用现金
				ractive.set("hdxj", "0.00");

				// 获得期限
				var csdm = Constants.CSDM.CSDM_TXJSQX;
				$.ajax({
					url : Constants.epUrlPath + "common/getXtcsszByCsdm?ywy="
							+ ywy + "&csdm=" + csdm,
					type : "GET",
					dataType : "json",
					contentType : "application/json",
					data : {},
					success : function(obj) {
						// 获取日期动态参数
						if (obj != null && obj.csz != "") {
							ractive.set("zrdayline", obj.csz);
						}

					}
				});

			}
		});

	}

	/* 计算可用现金 */
	function validate(obj) {
		// 校验数值
		var text = $(obj).val();
		if (text == null || $.trim(text).length == 0) {
			ractive.set("hdxj", "0.00");
			return;
		}
		// var reg=/^[0-9]+?$/;
		var reg = /^[1-9]\d*$/;
		if (!reg.test($.trim(text))) {
			alert("Please enter a positive integer!");
			$(obj).val("");
			ractive.set("hdxj", "0.00");
			return;
		}

		if (parseInt(text) < 0 || parseInt(text) > parseInt(jfye)) {
			var str = "Please fill in 1 ~" + jfye + " integer!";
			alert(str);
			$(obj).val("");
			ractive.set("hdxj", "0.00");
			return;
		}

		if (jfye == "0") {
			// 没有可用积分余额
			alert("There is no score balance available currently!");
			ractive.set("hdxj", "0.00");
			$(obj).val("");
			$(obj).attr("disabled", true);
			return;
		} else {

			// 最大金额
			var jfyeFloat = parseFloat(jfye);
			var cszFloat = parseFloat(csz);

			var max = (jfyeFloat / cszFloat).toFixed(3);
			var textFloat = parseFloat(text);
			var temp = (textFloat / cszFloat).toFixed(3);
			if ((temp - max) >= 0) {
				var rs = Math.floor(max * 100) / 100;
				if (rs == 0 || rs == 0.0) {
					ractive.set("hdxj", "0.00");
				} else {
					ractive.set("hdxj", rs.toFixed(2));
				}
			} else {
				var re = Math.floor(temp * 100) / 100;
				if (re == 0 || re == 0.0) {
					ractive.set("hdxj", "0.00");
				} else {
					ractive.set("hdxj", re.toFixed(2));
				}

			}

		}

	}

	return {
		init : function() {
			renderView();
		},
		validate : function(obj) {
			validate(obj);
		}
	};
}();
$(document).ready(yfmbgl_app.init);